package controller;

import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import process.ProcessDispatcher;

public class MainController {

    public Button openButton;
    public Button terminalButton;
    public Button fileButton;
    public Button processButton;
    public Button helpButton;

    public void buttonInit(){
//        this.openButton.addEventFilter(MouseDragEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
//            @Override
//            public void handle(MouseEvent event) {
//                openButton.setBackground(new Background(new BackgroundFill(Color.LIGHTSKYBLUE, null, null)));
//            }
//        });
//        this.openButton.addEventFilter(MouseDragEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
//            @Override
//            public void handle(MouseEvent event) {
//                openButton.setBackground(new Background(new BackgroundFill(null, null, null)));
//            }
//        });
        this.openButton.addEventFilter(MouseDragEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Platform.exit();
            }
        });
        this.terminalButton.addEventFilter(MouseDragEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getClickCount() == 2) {
                    Terminal t = new Terminal();
                    t.start(new Stage());
                    t.getHelp();
                }
            }
        });
        this.fileButton.addEventFilter(MouseDragEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getClickCount() == 2) {
                    (new FileResourceManagerController()).start(new Stage());
                }
            }
        });
        this.processButton.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getClickCount()==2) {
                    try {
                        FXMLLoader loader = new FXMLLoader();
                        loader.setLocation(getClass().getResource("/view/ProcessControlUI.fxml"));
                        Parent root = (Parent) loader.load();
                        Scene scene = new Scene(root);
                        //scene.getStylesheets().add("view/UIcss.css");
                        Stage stage=new Stage();
                        stage.setScene(scene);
                        stage.setTitle("进程调度");
                        stage.getIcons().add(new Image("/imgs/steam.png"));
                        stage.centerOnScreen();
                        stage.show();
//                        启动调度线程
                        ProcessDispatcher.dispatchProcess();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
        this.helpButton.addEventFilter(MouseDragEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                helpButton.setBackground(new Background(new BackgroundFill(Color.LIGHTSKYBLUE, null, null)));
            }
        });

    }
    public void init(Scene scene, Stage stage){
        //初始化多个组件
        buttonInit();
    }
}
